﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dapper;
using MySql.Data.MySqlClient;

namespace SHRMS.childForm
{
    /// <summary>
    /// 录入考勤
    /// </summary>
    public partial class childForm_attendanceAdd : Form
    {
        private readonly IDbConnection _dbConnection;
        public childForm_attendanceAdd()
        {
            InitializeComponent();
            _dbConnection = new DbFactory().DefaultDbConnection;
        }
        private void btn_AllInfo_Click(object sender, EventArgs e)
        {
            string sqlStr = "select * from t_attendance;";
            var result = _dbConnection.Query(sqlStr).ToList();
            dataGridView_att.DataSource = result;
            dataGridView_att.AutoResizeColumnHeadersHeight();
            dataGridView_att.AutoResizeColumns();
            dataGridView_att.AutoResizeRows();
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string sqlStr = "select * from t_attendance where empNo like '%" + textBox_str.Text + "%';";
            var result = _dbConnection.Query(sqlStr).ToList();
            dataGridView_att.DataSource = result;
            dataGridView_att.AutoResizeColumnHeadersHeight();
            dataGridView_att.AutoResizeColumns();
            dataGridView_att.AutoResizeRows();
        }

        private void dataGridView_att_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView_att.CurrentRow.Index;
            string empNo = dataGridView_att.Rows[index].Cells[0].Value.ToString();

            string sqlStr_getRole = $"select role from t_user where empNo = '{empNo}';";
            string role = _dbConnection.ExecuteScalar<string>(sqlStr_getRole);

            if (role == "教师")
            {
                string sqlStr_getBaseInfo = $@"select 
	tea.name,dept.deptName
from 
	t_teacher tea
join
	t_department dept
on
	tea.departmentNo = dept.departmentNo
where
	tea.teacherNo = '{empNo}';";

                var result = _dbConnection.ExecuteReader(sqlStr_getBaseInfo);
                result.Read();
                label_name.Text = result[0].ToString();
                label_dept.Text = result[1].ToString();
                result.Close();
                label_empNo.Text = empNo;
                label_role.Text = role;

                numericUpDown_overTime.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[3].Value);
                numericUpDown_late.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[4].Value);
                numericUpDown_earlyLeave.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[5].Value);
                numericUpDown_absenteeism.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[6].Value);
                numericUpDown_shouldDay.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[1].Value);
                numericUpDown_realDay.Value = Convert.ToInt32(dataGridView_att.Rows[index].Cells[2].Value);
            }
        }

        private void btn_modifyAtt_Click(object sender, EventArgs e)
        {
            string empNo = label_empNo.Text;
            string overtime = numericUpDown_overTime.Value.ToString();
            string late = numericUpDown_late.Value.ToString();
            string earlyLeave = numericUpDown_earlyLeave.Value.ToString();
            string absenteeism = numericUpDown_absenteeism.Value.ToString();
            string shouldDay = numericUpDown_shouldDay.Value.ToString();
            string realDay = numericUpDown_realDay.Value.ToString();

            string sqlStr_insert = $"update t_attendance set attendanceDays ='{shouldDay}',actualDays = '{realDay}',overtimeHours = '{overtime}',lateNumber = '{late}',earlyLeaveNumber= '{earlyLeave}',absenteeism = '{absenteeism}' where empNo = '{empNo}';";
            var result = _dbConnection.Execute(sqlStr_insert);
            if (result > 0)
            {
                MessageBox.Show("录入考勤成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("录入考勤失败！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
    }
}
